/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCardTemplateDuration;
import com.chlitina.store.modules.base.entity.StoreCardTemplateTimes;
import com.chlitina.store.modules.base.service.StoreCardTemplateDurationService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 时段护理卡模板Controller
 * @author liujiang
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeCardTemplateDuration")
public class StoreCardTemplateDurationController extends BaseController {

	@Autowired
	private StoreCardTemplateDurationService storeCardTemplateDurationService;
	
	@ModelAttribute
	public StoreCardTemplateDuration get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeCardTemplateDurationService.get(id);
		}else{
			return new StoreCardTemplateDuration();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(required=false)String currentSelectId, StoreCardTemplateDuration storeCardTemplateDuration, HttpServletRequest request, HttpServletResponse response, Model model) {
		String searchStatus = request.getParameter("searchStatus");
        Page<StoreCardTemplateDuration> page = storeCardTemplateDurationService.find(new Page<StoreCardTemplateDuration>(request, response), storeCardTemplateDuration, searchStatus); 
        model.addAttribute("searchStatus", searchStatus);
    	model.addAttribute("currentSelectId", currentSelectId);
        model.addAttribute("page", page);
		return "modules/base/storeCardTemplateDurationList";
	}

	@RequestMapping(value = "form")
	public String form(StoreCardTemplateDuration storeCardTemplateDuration, Model model) {
		model.addAttribute("storeCardTemplateDuration", storeCardTemplateDuration);
		return "modules/base/storeCardTemplateDurationForm";
	}

	@RequestMapping(value = "save")
	public String save(String pageNo, StoreCardTemplateDuration storeCardTemplateDuration, Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		storeCardTemplateDuration.setStore(store);
		if (!beanValidator(model, storeCardTemplateDuration)){
			return form(storeCardTemplateDuration, model);
		}
		storeCardTemplateDurationService.save(storeCardTemplateDuration);
		addMessage(redirectAttributes, "保存时段护理卡模板'" + storeCardTemplateDuration.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateDuration/?pageNo=" + pageNo +"&currentSelectId=" +storeCardTemplateDuration.getId();
	}
	
	@RequestMapping(value = "delete")
	public String delete(StoreCardTemplateDuration storeCardTemplateDuration, RedirectAttributes redirectAttributes) {
		int status = storeCardTemplateDuration.getStatus();
		if (status == StoreCardTemplateDuration.FLAG_ENABLE) {
			storeCardTemplateDuration.setStatus(StoreCardTemplateDuration.FLAG_DISABLE);
		} else {
			storeCardTemplateDuration.setStatus(StoreCardTemplateDuration.FLAG_ENABLE);
		}
		storeCardTemplateDurationService.save(storeCardTemplateDuration);
		addMessage(redirectAttributes, "时段护理卡模板状态更改成功！");
		return "redirect:"+Global.ADMIN_PATH+"/base/storeCardTemplateDuration/";
	}

}
